﻿using System;
using System.Collections.Generic;
using System.Text;

using Gx.Dal.Employee;//数据
using Gx.Model.Entity;//实体
using Gx.Common;//通用类
using Gx.Model.Enums;
using Gx.Bll.Shared;
using System.Linq;

namespace Gx.Bll.Employee
{
    public class G_StockOutGoodsInfoBll
    {
        public bool AddG_StockOutGoodsInfo(G_StockOutGoodsInfo model)
        {
            using (G_StockOutGoodsInfoDal dal = new G_StockOutGoodsInfoDal())
            {
                return dal.AddG_StockOutGoodsInfo(model);
            }
        }

        public bool DelG_StockOutGoodsInfo(int stockOutGoodsId)
        {
            using (G_StockOutGoodsInfoDal dal = new G_StockOutGoodsInfoDal())
            {
                return dal.DelG_StockOutGoodsInfo(stockOutGoodsId);
            }
        }

        public bool EditG_StockOutGoodsInfo(G_StockOutGoodsInfo model)
        {
            using (G_StockOutGoodsInfoDal dal = new G_StockOutGoodsInfoDal())
            {
                return dal.EditG_StockOutGoodsInfo(model);
            }
        }

        private List<G_StockOutGoodsInfo> GetG_StockOutGoodsInfoList(string strWhere)
        {
            using (G_StockOutGoodsInfoDal dal = new G_StockOutGoodsInfoDal())
            {
                return dal.GetG_StockOutGoodsInfoList(strWhere);
            }
        }

        public List<G_StockOutGoodsInfo> GetG_StockOutGoodsInfoList(int StockOutId)
        {
            using (G_StockOutGoodsInfoDal dal = new G_StockOutGoodsInfoDal())
            {
                StringBuilder strWhere = new StringBuilder();
                strWhere.AppendFormat(" AND StockOutId ={0}", StockOutId);
                return dal.GetG_StockOutGoodsInfoList(strWhere.ToString());
            }
        }

        public List<G_StockOutGoodsInfo> GetG_StockOutGoodsInfoDetails(int StockInId)
        {
            StringBuilder strWhere = new StringBuilder();
            strWhere.AppendFormat(" AND StockOutId={0}", StockInId);
            List<G_StockOutGoodsInfo> sogList = GetG_StockOutGoodsInfoList(strWhere.ToString());
            List<M_DictInfo> dictList = new M_DictInfoBll().GetDictInfoList((int)DictEnmus.计量单位);
            List<G_GoodsInfo> goodsList = new G_GoodsInfoBll().GetG_GoodsInfoList(sogList.Select(x => x.GoodsId).ToList());
            foreach (var item in sogList)
            {
                G_GoodsInfo goods = goodsList.Find(x => x.GoodsId == item.GoodsId);
                if (goods == null)
                {
                    item.GoodsName = "不存在或已被删除";
                    continue;
                }
                item.StrUnitValue = dictList.Exists(x => x.DictValue == item.UnitValue) ? dictList.Find(x => x.DictValue == item.UnitValue).DictName : "不存在或已被删除";
                item.ColorName = goods.ColorName;
                item.TypeName = goods.TypeName;
                item.Specs = goods.Specs;
                item.Num *= -1;
            }
            return sogList;
        }

        public List<G_StockOutGoodsInfo> GetG_StockOutGoodsInfoList(
        int pageIndex, int pageSize, out int totalCount)
        {
            using (G_StockOutGoodsInfoDal dal = new G_StockOutGoodsInfoDal())
            {
                int PageIndex = (pageIndex - 1) * pageSize + 1;
                int PageSize = pageIndex * pageSize;
                StringBuilder strWhere = new StringBuilder();
                return dal.GetG_StockOutGoodsInfoList(strWhere.ToString(), PageIndex, PageSize, out totalCount);
            }
        }


        private int GetG_StockOutGoodsInfoCount(string strWhere)
        {
            using (G_StockOutGoodsInfoDal dal = new G_StockOutGoodsInfoDal())
            {
                return dal.GetG_StockOutGoodsInfoCount(strWhere);
            }
        }

        public G_StockOutGoodsInfo GetG_StockOutGoodsInfoModel(int stockOutGoodsId)
        {
            using (G_StockOutGoodsInfoDal dal = new G_StockOutGoodsInfoDal())
            {
                return dal.GetG_StockOutGoodsInfoModel(stockOutGoodsId);
            }
        }

    }
}
